package procilege.test.pal;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

import java.util.Iterator;

import org.junit.Test;

import procilege.impl.ComputationalEntityMgr;
import procilege.lang.PrivilegeForm;
import procilege.lang.PrivilegeFormTerm;

import com.google.inject.Inject;

public class EntityIsATest1 extends AbstractPalTest {

	@Inject
	protected ComputationalEntityMgr cem;

	@Test
	public void test1() {

		mvm.give(nm("T0"));
		mvm.stepin();
		try {
			
		} finally {
			mvm.stepout();
		}
		

		def(nn("p1"), pvm(nn("a")));
		dumpHistory(0, 30);
		assertEquals(pvm(nn("a")), cem.retrievePrivlegeExpr(nn("p1")));

		PrivilegeForm pf1;

		pf1 = cem.retrievePrivlegeForm(nn("p1"));
		assertNotNull(pf1);
		int count = 0;
		Iterator<PrivilegeFormTerm> pftItr = pfa.normalTerms(pf1).iterator();
		while(pftItr.hasNext()) {
			PrivilegeFormTerm t = pftItr.next();
			if (nn("a").equals(t.getFunctionName())) {
				count++;
			}
		}
		assertEquals(1, count);

		def(nn("p1"), pvc(nn("a"), nn("b")));
		dumpHistory(0, 30);
		assertEquals(pvc(nn("a"), nn("b")), cem.retrievePrivlegeExpr(nn("p1")));

		pf1 = cem.retrievePrivlegeForm(nn("p1"));
		assertNotNull(pf1);
		count = 0;
		pftItr = pfa.normalTerms(pf1).iterator();
		while(pftItr.hasNext()) {
			PrivilegeFormTerm t = pftItr.next();
			if (nn("a").equals(t.getFunctionName())) {
				count++;
			} else if (nn("b").equals(t.getFunctionName())) {
				count++;
			}

		}
		assertEquals(2, count);

		// cem.updatePrivlegeExpr(, nn("p1"));
	}
}
